package cn.tradewin.advisor.components;

import java.util.List;

import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.Block;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Session;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.slf4j.Logger;
import org.tynamo.security.services.SecurityService;

import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.pages.login;
import cn.tradewin.advisor.persist.model.Account;
import cn.tradewin.advisor.persist.services.AccountService;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;

/**
 * Layout component for pages of application TwdPM.
 */
@Import(stylesheet = {"css/theme.css"})
public class Layout
{
    /**
     * The page title, for the <title> element and the <h1> element.
     */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block main;
    
    @Property
    private String pageName;
    
    @Property
    private String username;
    
    @Inject
    private ComponentResources resources;
    
    @Environmental
    JavaScriptSupport jss;
    
    @Inject
	Messages messages;
    
    @Inject
	private SecurityService securityService;
    
    @Inject
    private AccountService accountService;
    
    @Inject
    Request request;
    
    @Inject
    private Logger log;
    
    @AfterRender
    void afterRender() {
    	List<String> menus = Splitter.on("/").splitToList(resources.getPageName());
    	
    	
//    	jss.require("jquery");
    	jss.require("bootstrap");
//    	jss.require("menu").with(menuid);
    }

    /**
     * get the first leve menu item : top menu pages.
     * @return
     */
    public String[] getPageNames() {
    	return new String[] {"account/board", "project/board", "passets/checklist/codingboard"};
    }
    
    @SetupRender
    void setupRender() throws DBException {
    	Subject subject = securityService.getSubject();
		if (subject != null) {
			String userid = (String)subject.getPrincipal();
			Account account = accountService.findAccoutByID(userid);
			if (account != null) {
				username = Strings.nullToEmpty(account.getLastName()) + Strings.nullToEmpty(account.getFirstName());
			}
		}
    }
    
    public boolean disabled(String pageName) {
    	log.info("---------------------------------------------------------------------------------------------" + pageName);
    	return pageName.equalsIgnoreCase(resources.getPageName());
    }
    
    public String getNaviCss() {
    	String group = resources.getPageName().substring(0, resources.getPageName().indexOf("/"));
    	String menu = pageName.substring(0, pageName.indexOf("/"));
        return group.equalsIgnoreCase(menu) ? "active" : null;
	}
	
	public String getNaviLabel() {
		return messages.get(pageName);
	}
	
	public Object onLogout() {
		Subject subject = securityService.getSubject();
		if (subject != null) {
			subject.logout();
		}
		resources.discardPersistentFieldChanges();
		Session session = request.getSession(false);
		if (session != null) {
			session.invalidate();
		}
		return login.class;
	}
	
	
	public boolean isLogon() {
		return request.getSession(false) != null;
	}
}
